<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="drag" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主题布局 -->
        <el-tree :data="list" @node-click="handleNodeClick">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="money" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 准备弹层放入新增部门的子组件 -->
      <el-dialog
        :title="isEdit ? '编辑部门':'新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >

        <!-- 新增子部门编辑部门的子组件 -->
        <!-- 后期更新的数据是异步的  页面无法及时渲染 -->
        <!--方法一: v-if="showDialog" -->

        <addOrEdit
          :id="curId"
          ref="myDept"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="showDialog = false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, deleteRole } from '@/api/departments'
import { tranListToTreeList } from '@/utils/index'
import addOrEdit from './deptDialog.vue'

export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      showDialog: false,
      list: [],
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      console.log('弹层关闭')
      // 清空表单 表单在子组件里面
      this.$refs.myDept.restForm()
    },
    hDel(id) {
      this.$confirm('您确定要删除该部门吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteRole(id)
        console.log(res, 'deleteRole')
        if (res.success) {
          // 刷新页面
          this.loadDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 子传父 关闭弹层 渲染页面
    hSuccess() {
      // eslint-disable-next-line no-sequences
      this.showDialog = false,
      this.loadDepartments()
    },
    hEdit(id) {
      this.curId = id // 将id存储到data中在父传子
      this.isEdit = true // 将编辑状态改成true 确定当前是一个编辑操作
      this.showDialog = true
      // 方法二:
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentsDetail()
      })
    },
    hAdd(id) {
      this.curId = id// 将id存储到data中在父传子
      this.isEdit = false // 将编辑状态改成false 确定当前是一个新增操作
      this.showDialog = true
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 发送请求,获取数据
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, 'getDepartments')
        this.originList = res.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(this.originList, 'this.originList')
        res.data.depts.shift()
        this.list = tranListToTreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
